<template>
  <div>
    <!-- 返回按钮 -->
    <el-page-header @back="$router.go(-1)" content="工程绑定页面">
    </el-page-header>
    <!-- 工程信息 -->
    <div class="project-info">
      <div class="info-header">工程信息</div>
      <el-row>
        <el-col :span="6">
          <div class="info-label">工程编号：</div>
        </el-col>
        <el-col :span="18">
          <div class="info-value">{{ engObj.serial_number }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <div class="info-label">工程名称：</div>
        </el-col>
        <el-col :span="18">
          <div class="info-value">{{ engObj.name }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <div class="info-label">工程类型：</div>
        </el-col>
        <el-col :span="18">
          <div class="info-value">{{ engObj.type }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <div class="info-label">工程状态：</div>
        </el-col>
        <el-col :span="18">
          <div class="info-value">
            <el-tag v-if="engObj.status == '监测中'" type="success">{{ engObj.status }}</el-tag>
            <el-tag v-else-if="engObj.status == '未绑定'" type="warning">{{ engObj.status }}</el-tag>
            <el-tag v-else type="danger">{{ engObj.status }}</el-tag>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <div class="info-label">开始时间：</div>
        </el-col>
        <el-col :span="18">
          <div class="info-value">{{ engObj.create_time }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <div class="info-label">结束时间：</div>
        </el-col>
        <el-col :span="18">
          <div class="info-value">{{ engObj.end_time }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <div class="info-label">地址：</div>
        </el-col>
        <el-col :span="18">
          <div class="info-value">{{ engObj.address }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <div class="info-label">工程负责人：</div>
        </el-col>
        <el-col :span="18">
          <div class="info-value">{{ engObj.charge_id }}</div>
        </el-col>
      </el-row>
    </div>
    <!-- 绑定信息 -->
    <div class="binding-info">
      <div class="info-header" style="display: flex; justify-content: space-between; align-items: center;">
        <span>绑定信息</span>
        <div>
          <el-button type="primary" size="small" @click="bindDevice">绑定</el-button>
          <el-button type="info" size="small" @click="showAll = !showAll">
            {{ showAll ? '隐藏' : '显示所有设备' }}
          </el-button>
        </div>
      </div>
      <div v-if="showAll" class="all-devices">
        <div class="device-search">
          <el-input
            placeholder="请输入设备编号或名称"
            v-model="deviceSearch"
            clearable
            style="width: 300px; margin-bottom: 10px;"
            @keyup.enter.native="searchDevices"
          ></el-input>
          <el-button type="primary" @click="searchDevices">搜索</el-button>
        </div>
        <el-table :data="allDevices" style="width: 100%">
          <el-table-column type="index" :index="1" label="序号" width="60" align="center">
          </el-table-column>
          <el-table-column prop="device_id" label="设备编号" align="center">
          </el-table-column>
          <el-table-column prop="device_name" label="设备名称" align="center">
          </el-table-column>
          <el-table-column prop="online_status" label="在线状态" align="center">
            <template v-slot="scope">
              <el-tag v-if="scope.row.online_status == '在线'" type="success">{{ scope.row.online_status }}</el-tag>
              <el-tag v-else type="danger">{{ scope.row.online_status }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template v-slot="scope">
              <el-button type="success" size="small" @click="bindThisDevice(scope.row)">绑定</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-table v-else :data="bindData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="60" align="center">
        </el-table-column>
        <el-table-column prop="serial_number" label="设备编号" align="center">
        </el-table-column>
        <el-table-column prop="name" label="设备名称" align="center">
        </el-table-column>
        <el-table-column prop="latitude" label="经度" align="center">
        </el-table-column>
        <el-table-column prop="longitude" label="纬度" align="center">
        </el-table-column>
        <el-table-column prop="video" label="视频地址" align="center">
        </el-table-column>
        <el-table-column prop="online_status" label="在线状态" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.online_status == '在线'" type="success">{{ scope.row.online_status }}</el-tag>
            <el-tag v-else type="danger">{{ scope.row.online_status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <el-link :disabled="scope.row.online_status == '离线' ? true : false" type="primary"
              @click="toUnbinding(scope.row)">解绑</el-link>
            <el-link :disabled="scope.row.online_status == '离线' ? false : true" style="margin-left: 5px;" type="danger"
              @click="toOffline(scope.row)">下线</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { get, post } from '@/utils/request';

export default {
  data() {
    return {
      engObj: {},
      bindData: [],
      allDevices: [],
      deviceSearch: '',
      showAll: false
    };
  },
  created() {
    this.engObj = this.$route.query;
    this.getBindData();
  },
  methods: {
    async getBindData() {
      let res = await get('/device/pageQuery', {
        page: 1,
        pageSize: 10,
        engineer_id: this.engObj.id
      });
      this.bindData = res.data.list;
    },
    async getAllDevices() {
      let res = await get('/device/pageQuery', {
        page: 1,
        pageSize: 1000
      });
      this.allDevices = res.data.list;
    },
    async searchDevices() {
      if (!this.deviceSearch.trim()) {
        await this.getAllDevices();
        return;
      }
      let res = await get('/device/pageQuery', {
        page: 1,
        pageSize: 1000,
        device_id: this.deviceSearch,
        device_name: this.deviceSearch
      });
      this.allDevices = res.data.list;
    },
    async toUnbinding(row) {
      this.$confirm('此操作将解绑该设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await post('/device/unbinding', {
          device_id: row.device_id
        });
        this.$message({
          type: 'success',
          message: res.message
        });
        this.getBindData();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消解绑'
        });
      });
    },
    async toOffline(row) {
      this.$confirm('此操作将使设备下线, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await post('/device/offline', {
          device_id: row.device_id
        });
        this.$message({
          type: 'success',
          message: res.message
        });
        this.getBindData();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消下线'
        });
      });
    },
    bindDevice() {
      this.showAll = true;
    },
    async bindThisDevice(row) {
      let res = await post('/device/bind', {
        device_id: row.device_id,
        engineer_id: this.engObj.id
      });
      this.$message({
        type: 'success',
        message: res.message
      });
      this.showAll = false;
      this.getBindData();
    }
  }
};
</script>

<style lang="scss" scoped>
.project-info,
.binding-info {
  margin: 20px 0;
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.info-header {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.el-row {
  margin-bottom: 15px;
}

.info-label {
  font-weight: bold;
  color: #606266;
}

.info-value {
  color: #303133;
}

.all-devices {
  margin-top: 20px;
}

.device-search {
  display: flex;
  margin-bottom: 10px;
}
</style>